package yuan.algorithm.searching;

public class BinarySearch {
	
	public static int search(int[] input, int target){
		int begin = 0;
		int end = input.length-1;
		int current = 0;
		
		while(begin<=end){
			current = begin+(end-begin)/2;
			
			if (input[current] == target)
				return current;
			else if (input[current] > target)
				end = current-1;
			else
				begin = current+1;
		}
		
		return -1;
	}
	
	public static int search2(int[] inputs, int begin, int end, int key){
		if (begin>end)
			return -1;
		else{
			int current = begin+(end-begin)/2;
			if (inputs[current] == key)
				return current;
			else if (inputs[current] > key)
				end = current-1;
			else
				begin = current+1;
			
			return search2(inputs, begin, end, key);
		}
	}
}
